package com.hopu.v1.controller.front.question;

import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.question.ErrorNotebooksQuestions;
import com.hopu.v1.service.front.question.FErrorNotebooksQuestionsService;
import com.hopu.v1.utils.JwtUtils;
import com.hopu.v1.vo.JudgeErrQuestionVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ferrorNotebooks")
public class FErrorNotebooksQuestionsController {
    @Autowired
    private FErrorNotebooksQuestionsService fErrorNotebooksQuestionsService;

    //获取开始刷题的错题题目
    @GetMapping("/getErrQuestions")
    public R getErrQuestions(HttpServletRequest request){
        String token = request.getHeader("frontToken");
        String studentId = JwtUtils.getUserId(token);
        List<ErrorNotebooksQuestions> questions = fErrorNotebooksQuestionsService.queryErrQuestions(studentId);
        if (questions==null || questions.size()==0){
            return R.err("您已经更正所有错题了，暂时没有错题！！！");
        }
        return R.ok(questions);
    }

    //判断题目对错
    @PostMapping("/judgeErrQuestion")
    public R judgeErrQuestion(Integer questionId,String answer, HttpServletRequest request){
        String token = request.getHeader("frontToken");
        String studentId = JwtUtils.getUserId(token);
        JudgeErrQuestionVo judgeErrQuestionVo = fErrorNotebooksQuestionsService.judgeErrQuestion(studentId, questionId, answer);
        if (judgeErrQuestionVo==null){
            return R.err("非法入侵，请遵守法律");
        }else {
            return R.ok(judgeErrQuestionVo.getMessage(),judgeErrQuestionVo.getQuestions());
        }
    }
}
